package com.operation.net.scheduledtask;

import java.util.TimerTask;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TestMySQL extends TimerTask{



        private Connection connection;
        private String JDBC_Driver = "com.mysql.cj.jdbc.Driver";
        private String DB_URL = "jdbc:mysql://:3306/network";
        private String user = "root";
        private String password = "";
        //时间量
        Date startDate = new Date();
        //定时器
        private Timer timer = new Timer();
        public void start(){
            //2秒执行一次
            timer.schedule(this,startDate,2000);
        }
        public void run() {
            double round = Math.round(1);
            System.out.println("round:"+round);
            try {
                Class.forName(JDBC_Driver);
                connection = DriverManager.getConnection(DB_URL,user,password);
                String sql = "insert into t_test(num) values (?)";
                PreparedStatement rs = connection.prepareStatement(sql);
                rs.setDouble(1,round);
                rs.executeUpdate();
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    public static void main(String[] args) {
        TestMySQL testMySQL = new TestMySQL();
        testMySQL.start();
        System.out.println("testMysql:"+testMySQL);
    }
    }


